﻿// (c)2010 David Schwarz (david.schwarz@live.com)
// This file is licensed under GNU GPL v2 license, see more at http://www.gnu.org/licenses/gpl-2.0.html
namespace Z2.Core
{

	#region Using

	using System;
	using Microsoft.Xna.Framework;
	using Microsoft.Xna.Framework.Graphics;
	using Microsoft.Xna.Framework.Input;

	#endregion

	/// <summary>
	/// Kurzor ve hře
	/// </summary>
	public class GameCursor
	{

		#region Const

		private const string GRAPHICS = "leaf";

		#endregion

		#region Members

		private Sprite m_sprite;
		private Vector2 m_position;
		private ButtonState m_state;

		#endregion

		#region Costructor

		/// <summary>
		/// Konstruktor
		/// </summary>
		public GameCursor()
		{
			this.m_sprite = SpriteManager.Instance.LoadSprite(GameCursor.GRAPHICS, 1, 1);
			this.m_position = new Vector2();
			this.m_state = ButtonState.Released;
		}

		#endregion

		#region Properties

		/// <summary>
		/// Vrací pozici kurzoru
		/// </summary>
		public Vector2 Position
		{
			get
			{
				return this.m_position;
			}
		}

		#endregion

		#region Events

		/// <summary>
		/// Událost nastane po kliknutí na levé tlačítko myši
		/// </summary>
		public event EventHandler LeftMouseDown;

		#endregion

		#region Methods

		/// <summary>
		/// Metoda aktualizuje pozici myši
		/// </summary>
		public void Update()
		{
			MouseState state = Mouse.GetState();
			this.m_position.X = state.X;
			this.m_position.Y = state.Y;

			if (state.LeftButton == ButtonState.Pressed && this.m_state == ButtonState.Released)
			{
				this.OnLeftMouseDown();
			}

			if (state.LeftButton == ButtonState.Released && this.m_state == ButtonState.Pressed)
			{
				this.m_state = ButtonState.Released;
			}
		}

		/// <summary>
		/// Metoda vyvolá událost <see cref="LeftMouseDown"/>
		/// </summary>
		private void OnLeftMouseDown()
		{
			this.m_state = ButtonState.Pressed;
			if (this.LeftMouseDown == null)
			{
				return;
			}

			this.LeftMouseDown(this, EventArgs.Empty);
		}

		/// <summary>
		/// Metoda vykreslí kurzor
		/// </summary>
		/// <param name="batch"><see cref="SpriteBatch"/></param>
		public void Draw(SpriteBatch batch)
		{
			this.m_sprite.Draw(batch, this.m_position);
		}

		#endregion

	}
}
